package guoanmaker.operator.business.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import guoanmaker.operator.business.service.OperatorCustomersServiceIntegerface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(ApiController.OPERATOR_CUSTOMERS)
public class OperatorCustomersController {
	@Autowired
	 private  AjaxJson ajaxJson;
	@Autowired
	private OperatorCustomersServiceIntegerface operatorCustomersServiceIntegerface;
	@ApiOperation(value = "导入消息客群人员excele", notes = "导入消息客群人员excele")
	    @PostMapping(value = "importCustomersData")
	public AjaxJson importCustomersData(HttpServletRequest request, HttpServletResponse response, MultipartFile dataFile,String peopleType) {
		Map<String, Object> map=new HashMap<String, Object>();
		int a=0;
		System.out.println(peopleType+"选中");
		try (InputStream in = dataFile.getInputStream();
				Workbook wb = WorkbookFactory.create(in);) {
			
			Sheet sheet0 = wb.getSheetAt(0);
			Row row1 = sheet0.getRow(0);
			if(row1.getCell(0).getStringCellValue().equals("手机号")&&
			row1.getCell(1).getStringCellValue().equals("邮箱")){
				 map=operatorCustomersServiceIntegerface.doImportCustomerData(request, response, wb, dataFile,peopleType);
				 a=1;
		}else{
				a=2;
			}
	 
	  map.put("a", a);
			
		} catch (IOException | EncryptedDocumentException
				| InvalidFormatException e) {
			
			 map.put("a", 3);
				
	}
		ajaxJson.setData(map);
		return ajaxJson;
	}
	
	/**
	 * dealnumType交易次数类型 1是大于 2是小于 3 等于
	 * moneyType 交易金额类型 1 是大于 2是小于 3是等于
	 * peppleType 1 是用户 2是商户
	* @Title: selectCustomersData 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param peppleType
	* @param resbeginTime
	* @param resendTime
	* @param dealnum
	* @param money
	* @param dealnumType
	* @param moneyType
	* @return    设定文件 
	* @return AjaxJson    返回类型 
	* @throws 
	 */
	@ApiOperation(value = "消息人员筛选", notes = "消息人员筛选")
    @PostMapping(value = "selectCustomersData")
public AjaxJson selectCustomersData(HttpServletRequest request, HttpServletResponse response,String peppleType,String resbeginTime,String resendTime,Integer dealnum ,Double money,Integer dealnumType,Integer moneyType) {
		try{
		Map remap=operatorCustomersServiceIntegerface.selectUserList(request,response,peppleType,resbeginTime,resendTime,dealnum,money,dealnumType,moneyType);
		ajaxJson.setSuccess(true).setStatus(200).setData(remap);
		}catch(Exception e){
			ajaxJson.setSuccess(false).setStatus(250).setData(null);
			e.printStackTrace();
		}
		return ajaxJson;
	}
	
	
	

	
}
